// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import EventManager from "../../../Common/EventManager";
import Loader from "../../../Common/Loader";
import yyComponent from "../../../Common/yyComponent";
import { EventType } from "../../../GameSpecial/GameEventType";
import { mgrs, sers } from "../../system/OverallSystem ";
import { GAME_MODE } from "../game/gameMode_Json";

const {ccclass, property} = cc._decorator;

@ccclass
export default class gameListItem extends yyComponent{

   

    // onLoad () {}

    @property(cc.Sprite)
    icon:cc.Sprite = null

    @property(cc.Node)
    vicon:cc.Node = null

    @property(cc.Label)
    gameNmae:cc.Label = null


    @property(cc.Node)
    lockNode:cc.Node = null

    @property(cc.Material)
    huise:cc.Material = null

    @property(cc.Material)
    zhengchang:cc.Material = null


    start () {
        // this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)

        this.on(EventType.UIEvent.updataGamelist,this.updataView,this)
    }
    data = null
    setData(data){
        cc.log(data)
       this.data = data
       this.updataView()
    }

    updataView(){
        if(!this.data)return
        let data = this.data
        this.gameNmae.string = `${data.name}`


        let s = sers().userSrv.getGuanqiaGameList()
        if(s.indexOf(+data.key)>=0 || +data.key==1){
            this.vicon.active = false
            this.lockNode.active = true
        }

    //    let m = this.lockNode.active ? this.zhengchang : this.huise

    //    this.icon.setMaterial(0,m)
    //     this.icon.node.children[0].getComponent(cc.Sprite).setMaterial(0,m)

        Loader.loadBundleRes('Majiang', 'guanqiaicon/'+this.data.id, (res) => {
        //    cc.log(res)
            if(!this || !this.node)return
            this.icon.spriteFrame = new cc.SpriteFrame(res)
        })
    }

    onTouchStart(){
        mgrs().soundMgr.playBtnSound();

        if(this.vicon.active){
            sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
                let s = sers().userSrv.getGuanqiaGameList()
                s.push(+this.data.key)
                sers().userSrv.setGuanqiaGameList(s)
                EventManager.emit(EventType.UIEvent.updataGamelist)
            }, (err) => {
            }, null,  {
                scene: '关卡模式解锁',
                event: '解锁'+this.data.name,
              },'common_video');
        }else{
            // sers().userSrv.setGameList(this.data.gameType)
            // mgrs().gameMgr.enterMode(this.data.gameType);
            window['guanqiaMODE'] = this.data.id
            window['guanqiaMODE_name'] = this.data.name
            window['guanqiaMODE_key'] = this.data.key
            mgrs().gameMgr.enterMode(GAME_MODE.MAJIANGLEVEL);
        }
    }

    // update (dt) {}
}
